package com.two.lbms.exceptions;

import com.alibaba.fastjson.JSON;
import com.two.lbms.base.ResultInfo;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author hjx
 * @version V1.0
 * @Date 2023/7/18 21:20
 */
@Component
@Slf4j
public class GlobalExceptionResolver implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {

        log.info("进入全局异常");
        //返回json样式
        response.setContentType("application/json;charset=utf-8");
        response.setCharacterEncoding("utf-8");

        if (ex instanceof NoLoginException) {
            // 如果捕获的是未登录异常，则重定向到登录⻚⾯
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("redirect:/login");
            return modelAndView;
        }

        ModelAndView view = new ModelAndView();
        view.setViewName("");
        view.addObject("code", 400);
        view.addObject("msg", "系统异常");

        if (handler instanceof HandlerMethod) {
            HandlerMethod  handlerMethod = (HandlerMethod) handler;
            ResponseBody responseBody = handlerMethod.getMethod().getDeclaredAnnotation(ResponseBody.class);
            RestController restController = handlerMethod.getBean().getClass().getDeclaredAnnotation(RestController.class);
            if (responseBody == null && restController == null) {

                if (ex instanceof  Exception) {
                    ParamsException p = (ParamsException) ex;
                    view.addObject("code", p.getCode());
                    view.addObject("msg", p.getMessage());
                }
                return view;
            } else {

                ResultInfo resultInfo = new ResultInfo();
                resultInfo.setCode(400);
                resultInfo.setMsg("程序处理失败");
                if (ex instanceof  ParamsException) {
                    ParamsException p = (ParamsException) ex;
                    resultInfo.setCode(p.getCode());
                    resultInfo.setMsg(p.getMessage());
                }

                log.info("程序处理失败 : {}", resultInfo);
                try (PrintWriter out = response.getWriter()) {
                    // 将对象转换成JSON格式，通过输出流输出
                    out.write(JSON.toJSONString(resultInfo));
                    out.flush();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;

            }
        }
        return view;
    }
}
